﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;

public partial class Management_CompanyRecharge : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            panel_Money.Visible = false;
             var _key = Request.QueryString["key"] == null ? "" : Request.QueryString["key"].ToString();
             if (!string.IsNullOrEmpty(_key))
             {
                 txt_KeyWord.Text = _key;
                 this.Search();
             }
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        this.Search();
    }
    protected void Search()
    {
        if (string.IsNullOrEmpty(txt_KeyWord.Text)) throw new Exception("关键字不能为空");

        using (var _adc = DBHelper.GetDataContext())
        {
            gv_Company.DataSource = _adc.CompanyInformation.Where(i => i.CompanyName.Contains(txt_KeyWord.Text) || i.User.Contains(txt_KeyWord.Text));
            gv_Company.DataBind();
        }
    }
    protected void btn_Select_Click(object sender, EventArgs e)
    {
        try
        {
            var _btn = sender as Button;
            btn_Recharge.CommandArgument = _btn.CommandArgument;
            panel_Money.Visible = true;
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "选择失败:" + _e.Message;
        }
    }
    protected CompanyFinancialAccount GetBalance(string _companyName)
    {
        return DBHelper.Get<CompanyFinancialAccount>(a => a.Company == _companyName, "获取账户余额出错");
    }
    protected void btn_Recharge_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                if (string.IsNullOrEmpty(txt_Money.Text)) throw new Exception("充值金额不能为空");

                var _btn = sender as Button;
                var _companyID = _btn.CommandArgument;
                var _money = Convert.ToDecimal(txt_Money.Text);

                if (string.IsNullOrEmpty(_companyID)) throw new Exception("企业用户名称不能为空");

                var _balance = _adc.CompanyFinancialAccount.Where(a => a.Company == _companyID).FirstOrDefault();
                if (_balance != null)
                {
                    _balance.Balance += _money;
                }
                else
                {
                    _balance = new CompanyFinancialAccount();
                    _balance.Balance = _money;
                    _balance.Company = _companyID;

                    _adc.CompanyFinancialAccount.InsertOnSubmit(_balance);
                }

                CompanyFinancialAccountLog _log = new CompanyFinancialAccountLog();
                _log.Date = DateTime.Now;
                _log.AccountCategoryID = 1;
                _log.Company = _companyID;
                _log.Money = _money;
                _log.Balance = _balance.Balance;

                _adc.CompanyFinancialAccountLog.InsertOnSubmit(_log);
                _adc.SubmitChanges();
            }
            txt_Money.Text = "";
            panel_Money.Visible = false;

            this.Search();
        }
        catch (Exception _e)
        {
            lbl_Error.Text="充值出错:"+_e.Message;
        }

    }
}
